<?php


namespace App\Http\Controllers\Api;


use App\Services\Business\Models\Conquer;
use App\Services\Business\Models\UserConquer;
use App\Services\Business\Models\UserRelative;

use App\Services\Member\Models\Family;
use App\Services\Member\Models\User;


use App\Zesow\WechatMini;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Constant\ApiV2Constant;
use App\Businesses\BusinessState;
use App\Constant\FrontendConstant;
use App\Services\Base\Services\ConfigService;
use App\Services\Member\Services\UserService;
use App\Services\Member\Services\SocialiteService;
use App\Services\Member\Services\ActivityService;
use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Interfaces\SocialiteServiceInterface;




class UserController extends BaseController
{
    /**
     * @var UserService
     */
    protected $userService;


    /**
     * @var ActivityService
     */
    protected $activityService;

    /**
     * @var SocialiteService
     */
    protected $socialiteService;

    protected $businessState;
    /**
     * @var ConfigService
     */
    protected $configService;



    public function __construct(
        UserServiceInterface         $userService,
        BusinessState                $businessState,
        ConfigServiceInterface       $configService
    )
    {
        $this->userService = $userService;
        $this->businessState = $businessState;
        $this->configService = $configService;


    }

    public function family($id)
    {

        $family  = Family::query()->where('user_id',$id)->first();

        return $this->successData(compact('family'));


    }
    public function detail(Request $request, $userId,BusinessState $businessState, SocialiteServiceInterface $socialiteService)
    {

        /**
         * @var SocialiteService $socialiteService
         */



        $user = $this->userService->find($userId, ['roles', 'profile']);

        $user = arr1_clear($user, ApiV2Constant::MODEL_MEMBER_FIELD);

        $socialites = $socialiteService->userSocialites($userId);

        $socialites = array_column($socialites, null, 'app');

        // 是否绑定QQ
        $user['is_bind_qq'] = isset($socialites[FrontendConstant::SOCIALITE_APP_QQ]) ? 1 : 0;
        // 是否绑定微信
        $user['is_bind_wechat'] = isset($socialites[FrontendConstant::WECHAT_LOGIN_SIGN]) ? 1 : 0;
        // 是否绑定手机号
        $user['is_bind_mobile'] = $businessState->isNeedBindMobile($user) ? 0 : 1;


        // 关注人数
        $user['follower_count'] = $this->userService->followingCount($userId);
        // 粉比人数
        $user['following_count'] = $this->userService->followerCount($userId);
        //是否有家庭
        $user['has_family'] = $this->userService->hasFamily($userId);
        $isFollowed = false;

        if ($this->check()) {
            $isFollowed = $this->userService->isFollowed($this->id(), $userId);
        }

        return $this->successData(compact('user', 'isFollowed'));
    }



    /**
     * 我的亲友
     * @param Request $request
     * @return JsonResponse
     */
    public function relative(Request $request,$userId)
    {


        $size = $request->input('size');
        $user = User::query()->where('id',$userId)->first();
        $excludeIds =  $request->input('exclude_ids');
        $relatives = UserRelative::query()->with(['relative:id,nick_name,avatar','user:id,nick_name,avatar',])
            ->when($excludeIds,function ($query) use($excludeIds){
                return $query->whereNotIn('relative_id',$excludeIds);
            })
            ->where('user_id', $userId)->paginate($size);
        return $this->successData(compact('relatives','user'));

    }


    /**
     * 动态
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function activity(Request $request, $id)
    {

        $page = (int)$request->input('page', 1);
        $size = (int)$request->input('size', 10);
        $activities = $this->userService->getActivities($id, $page, $size);
        return $this->successData(compact('activities'));

    }

    /**
     * 用户挑战的动态
     * @param Request $request
     * @param $id
     * @param $conquerId
     * @return JsonResponse
     */
    function  weightFragmentByConquer(Request $request, $id, $conquerId)
    {


        $page = (int)$request->input('page', 1);
        $size = (int)$request->input('size', 10);
        [
            'total' => $total,
            'list' => $list,
        ] = $this->userService->getUserWeightFragmentByConquer($id,$conquerId, $page, $size);
        $fragments = $this->paginator($list, $total, $page, $size);
        return $this->successData(compact('fragments'));


    }
    public  function  weightFragmentTrendByConquer(Request $request,$userId,$conquerId)
    {


        $days =  $request->input('days',7);
        $fragments = $this->userService->getUserWeightFragmentTrendByConquer($userId,$conquerId,$days);
        return $this->successData(compact('fragments'));


    }

    function  conquer(Request $request, $id, $conquerId)
    {


        $conquer = UserConquer::query()->with(['conquer','user'])->where('user_id',$id)->where('conquer_id',$conquerId)->first();
        $isFollowed = false;
        $isPublishedToday = false;
        $isThumbsuped = false;
        if ($this->check()) {
            $isFollowed = $this->userService->isFollowed($this->id(), $id);
            $isPublishedToday = $this->userService->isPublishedToday($id, $this->id());
            $isThumbsuped = $this->userService->isThumbsuped($this->id(), $id, Conquer::class);
        }
        return $this->successData(compact('conquer','isFollowed','isPublishedToday','isThumbsuped'));


    }

    function run(Request $request,$userId)
    {

        $page = (int)$request->input('page', 1);
        $size = (int)$request->input('size', 10);
        [
            'total' => $total,
            'list' => $list,
        ] = $this->userService->getUserRuns($userId, $page, $size);
        $doubts = $this->paginator($list, $total, $page, $size);

        return $this->successData($doubts);

    }




    /**
     * @param Request $request
     * @return JsonResponse
     */
    function conquers(Request $request,$id)
    {

        $page = (int)$request->input('page', 1);
        $size = (int)$request->input('size', 10);
        [
            'total' => $total,
            'list' => $list,
        ] = $this->userService->getUserConquers($id, $page, $size);
        $doubts = $this->paginator($list, $total, $page, $size);

        return $this->successData($doubts);

    }




}
